import { getRepository } from 'typeorm';
import { NextFunction, Request, Response } from 'express';
import { Role } from '../entity/Role';
import { User } from '../entity/User';

import { successRes, failRes } from '../utils/response';
export class RoleController {
  private roleRepository = getRepository(Role);
  //   private userRepository = getRepository(User);

  async list(request: Request, response: Response, next: NextFunction) {
    const { pageNo, pageSize, roleName } = request.query;
    var data = [];
    var total = 0;
    try {
      let queryBuilder = this.roleRepository.createQueryBuilder('role');
      if (roleName) {
        queryBuilder = queryBuilder.andWhere('role.roleName like :roleName', { roleName: '%' + roleName + '%' });
      }
      queryBuilder = queryBuilder.loadRelationCountAndMap('role.usersCount', 'role.users');
      queryBuilder = queryBuilder.leftJoinAndSelect('role.users', 'users');
      queryBuilder = queryBuilder.skip((pageNo - 1) * pageSize).take(pageSize);

      // console.log('sql', sql);
      data = await queryBuilder.getMany();
      total = await queryBuilder.getCount();
      let result = {
        pageNo: pageNo,
        records: data,
        size: pageSize,
        total: total,
      };
      return successRes(result);
    } catch (error) {
      return failRes(error);
    }
  }
  async one(request: Request, response: Response, next: NextFunction) {
    try {
      console.log('request.query.id', request.query);
      let result = await this.roleRepository.findOne(request.query.id);
      return successRes(result);
    } catch (error) {
      return failRes(error);
    }
  }

  async add(request: Request, response: Response, next: NextFunction) {
    try {
      const toSave = request.body;
      let role = await this.roleRepository.findOne({ roleName: toSave.roleName });
      if (role) {
        return {
          message: '添加失败,系统中已存在该用户',
          success: false,
        };
      }
      let obj = await this.roleRepository.save(toSave);
      if (obj.id) {
        return successRes('添加成功');
      } else {
        return failRes('添加失败');
      }
    } catch (error) {
      return failRes(error);
    }
  }
  async edit(request: Request, response: Response, next: NextFunction) {
    try {
      const toSave = request.body;
      let result = await this.roleRepository.findOne(toSave.id);
      if (result) {
        await this.roleRepository.save(toSave);
        return successRes('编辑成功');
      } else {
        return failRes({}, 500, '未找到该记录');
      }
    } catch (error) {
      return failRes(error);
    }
  }

  async remove(request: Request, response: Response, next: NextFunction) {
    try {
      let newsToRemove = await this.roleRepository.findOne(request.query.id);

      if (newsToRemove) {
        await this.roleRepository.remove(newsToRemove);
        return successRes({}, '删除成功');
      } else {
        return failRes({}, 500, '未找到该记录');
      }
    } catch (error) {
      return failRes(error);
    }
  }
}
